import loader from '@monaco-editor/loader'
import { ref } from 'vue'

loader.config({
  paths: {
    vs: "https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.34.0/min/vs", // 国内地第三方cdn服务
  },
});
// vs: "https://cdn.jsdelivr.net/npm/monaco-editor@0.52.0/min/vs", // 国内地第三方cdn服务
const monacoRef = ref(null)
const initMonaco = () => {
  return new Promise((resolve, reject) => {
    if (monacoRef.value) {
      resolve()
      return
    }

    loader.init()
        .then((monacoInstance) => {
          monacoRef.value = monacoInstance
        })
        .catch((error) => {
          if (error?.type !== 'cancelation') {
            console.error('Monaco initialization error:', error)
            //alert(3+JSON.stringify(error))
            reject()
          }
        })
  })
}

export function useMonacoEditor() {
  return {
    initMonaco,
    monacoRef
  }
}
